package com.smart.community.property.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * 物业费视图对象
 * 
 * @author Wu.Liang
 * @version 1.0.0
 * @since 2025-06-26
 */
@Data
public class PropertyFeeVO implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    /** 主键ID */
    private Long id;
    
    /** 费用编号 */
    private String feeNo;
    
    /** 用户ID */
    private Long userId;
    
    /** 用户姓名 */
    private String userName;
    
    /** 社区ID */
    private Long communityId;
    
    /** 社区名称 */
    private String communityName;
    
    /** 楼栋ID */
    private Long buildingId;
    
    /** 楼栋名称 */
    private String buildingName;
    
    /** 单元ID */
    private Long unitId;
    
    /** 单元名称 */
    private String unitName;
    
    /** 房屋ID */
    private Long householdId;
    
    /** 房户号 */
    private String roomNumber;
    
    /** 房户地址（格式：{社区名}-{楼栋名}-{单元名}） */
    private String householdAddress;
    
    /** 在线支付ID */
    private Long onlinePaymentId;
    
    /** 费用名称 */
    private String feeName;
    
    /** 费用计费方式：0-按面积计费，1-固定费用 */
    private Integer feeType;
    
    /** 费用类型名称 */
    private String feeTypeName;
    
    /** 费用金额 */
    private BigDecimal amount;
    
    /** 单价 */
    private BigDecimal unitPrice;
    
    /** 房屋面积 */
    private BigDecimal area;
    
    /** 已缴金额 */
    private BigDecimal paymentAmount;
    
    /** 剩余金额 */
    private BigDecimal remainingAmount;
    
    /** 计费周期 */
    private String billingPeriod;
    
    /** 缴费截止日期 */
    @JsonFormat(pattern = "yyyy-MM-dd")
    private LocalDate dueDate;
    
    /** 缴费状态：0-未缴费，1-已缴费，2-部分缴费，3-逾期 */
    private Integer paymentStatus;
    
    /** 缴费状态名称 */
    private String paymentStatusName;
    
    /** 缴费方式：wechat-微信，alipay-支付宝，bank-银行卡，balance-余额 */
    private String paymentMethod;
    
    /** 缴费方式名称 */
    private String paymentMethodName;
    
    /** 缴费时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime paymentTime;
    
    /** 是否逾期 */
    private Boolean isOverdue;
    
    /** 逾期天数 */
    private Integer overdueDays;
    
    /** 用户手机号 */
    private String userPhone;
    
    /** 备注 */
    private String remark;
    
    /** 创建时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;
    
    /** 更新时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updateTime;
    
    /**
     * 获取费用类型名称
     */
    public String getFeeTypeName() {
        if (feeType == null) {
            return "未知";
        }
        switch (feeType) {
            case 1:
                return "物业费";
            case 2:
                return "水费";
            case 3:
                return "电费";
            case 4:
                return "燃气费";
            case 5:
                return "卫生费";
            default:
                return "未知";
        }
    }
    
    /**
     * 获取缴费状态名称
     */
    public String getPaymentStatusName() {
        if (paymentStatus == null) {
            return "未知";
        }
        switch (paymentStatus) {
            case 0:
                return "未缴费";
            case 1:
                return "已缴费";
            case 2:
                return "部分缴费";
            case 3:
                return "逾期";
            default:
                return "未知";
        }
    }
    
    /**
     * 获取缴费方式名称
     */
    public String getPaymentMethodName() {
        if (paymentMethod == null) {
            return "未知";
        }
        switch (paymentMethod) {
            case "wechat":
                return "微信支付";
            case "alipay":
                return "支付宝";
            case "bank":
                return "银行卡";
            case "balance":
                return "余额支付";
            default:
                return "未知";
        }
    }
    
    /**
     * 判断是否已完全缴费
     */
    public boolean isFullyPaid() {
        return remainingAmount != null && remainingAmount.compareTo(BigDecimal.ZERO) == 0;
    }
    
    /**
     * 计算剩余金额
     */
    public void calculateRemainingAmount() {
        if (amount != null && paymentAmount != null) {
            this.remainingAmount = amount.subtract(paymentAmount);
        } else if (amount != null) {
            this.remainingAmount = amount;
        } else {
            this.remainingAmount = BigDecimal.ZERO;
        }
    }
    
    /**
     * 计算逾期天数
     */
    public void calculateOverdueDays() {
        if (dueDate != null && !isFullyPaid()) {
            LocalDate now = LocalDate.now();
            if (now.isAfter(dueDate)) {
                this.overdueDays = (int) (now.toEpochDay() - dueDate.toEpochDay());
                this.isOverdue = true;
            } else {
                this.overdueDays = 0;
                this.isOverdue = false;
            }
        } else {
            this.overdueDays = 0;
            this.isOverdue = false;
        }
    }
}
